#include "Factory.h"

using namespace SG::Base;

//=============================================================================
// Producer

Producer::Producer ()
{}

Producer::~Producer ()
{}

//=============================================================================
// Factory

Factory::Factory ()
{}

Factory::~Factory ()
{
    for (auto pI = myProducers.begin (); pI != myProducers.end (); ++pI)
        delete pI->second;
}

Factory* Factory::GetInstance ()
{
    static Factory factory;
    return &factory;
}

void* Factory::Produce (const std::string& sClassName) const
{
    auto pProd = myProducers.find (sClassName);
    if (pProd != myProducers.end ())
        return pProd->second->Produce ();
    else
        return nullptr;
}

void Factory::AddProducer (const std::string& sClassName, Producer* pcProducer)
{
    myProducers[sClassName] = pcProducer;
}

bool Factory::CanProduce (const std::string& sClassName) const
{
    return (myProducers.find (sClassName) != myProducers.end ());
}

std::list<std::string> Factory::CanProduce () const
{
    std::list<std::string> lObjects;

    for (auto pI = myProducers.begin (); pI != myProducers.end (); ++pI)
    {
        lObjects.push_back (pI->first);
    }

    return lObjects;
}
